/*
 * Copyright (2022) Bytedance Ltd. and/or its affiliates
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <Optimizer/Property/Property.h>
#include <QueryPlan/PlanVisitor.h>
#include <QueryPlan/TableWriteStep.h>
#include <QueryPlan/TotalsHavingStep.h>

#include <utility>

namespace DB
{
class PropertyDeterminer
{
public:
    static PropertySets determineRequiredProperty(QueryPlanStepPtr step, const Property & property, Context & context);
};

class DeterminerContext
{
public:
    DeterminerContext(Property required_, Context & context_) : required(required_), context(context_) { }
    Property getRequired() const { return required; }
    Context & getContext() const { return context; }

private:
    Property required;
    Context & context;
};

class DeterminerVisitor : public StepVisitor<PropertySets, DeterminerContext>
{
public:
    PropertySets visitStep(const IQueryPlanStep &, DeterminerContext &) override;

#define VISITOR_DEF(TYPE) PropertySets visit##TYPE##Step(const TYPE##Step &, DeterminerContext &) override;
    APPLY_STEP_TYPES(VISITOR_DEF)
#undef VISITOR_DEF

private:
    static PropertySet single()
    {
        return {Property{Partitioning{Partitioning::Handle::SINGLE}, Partitioning{Partitioning::Handle::SINGLE}}};
    }
};

}
